<?php
   
class API_Numbers extends API_Class {
                    
    public static $table = 'numbers',
                  $tableTar = 'tariffs',
                  $viewStat = 'statistic_view';
    
    public function getNumbersWhere($where){
        $q = "select n.*, t.name from ".self::$table." n, ".self::$tableTar." t where t.tariff_id=n.tariff_id and ".$where;
        $ret = self::$db->fetchAll($q);  
        if(!$ret)return array();
        return $ret; //API_Utils::ansi_to_utf($ret);
    }
    
    public function getNumbersByClient($cid){
        return self::getNumbersWhere("CLIENT_ID = '$cid'");
    }
    
    public function normNum($phone){
        return ereg_replace("[^0-9]", "", $phone);
    }
    
    public function isClientsNumber($cid, $phone){
        $q = "select number_id from ".self::$table." where CLIENT_ID = '$cid' and NUM = '$phone'";  
        $ret = self::$db->fetchRow($q);
        return $ret?true:false;
    }
    
    public function getStatistic($phone){
        /*$q = "SELECT     CONNECTIONS.CONNECTION_ID, CONNECTIONS.NUMBER_ID, CONNECTIONS.TARGET_NUMBER, CONNECTIONS.CONNECTION_DATE, 
              CONNECTIONS.DURATION, CONNECTIONS.TARGET_PREFIX, (CONNECTIONS.DURATION * RATE_SCHEDULE.PRICE) AS COST
              FROM         RATE_SCHEDULE, DIRECTIONS, TARIFFS, CONNECTIONS, NUMBERS
              WHERE     RATE_SCHEDULE.DIRECTION_ID = DIRECTIONS.DIRECTION_ID AND RATE_SCHEDULE.TARIFF_ID = TARIFFS.TARIFF_ID AND 
              DIRECTIONS.PREFIX = CONNECTIONS.TARGET_PREFIX AND CONNECTIONS.NUMBER_ID = NUMBERS.NUMBER_ID AND 
              TARIFFS.TARIFF_ID = NUMBERS.TARIFF_ID AND (NUMBERS.NUM = '$phone') AND (CONNECTIONS.CONNECTION_DATE >
              (SELECT     SYSDATE - 30 AS EXPR1
              FROM          SYS.\"DUAL\"))
              ORDER BY CONNECTION_DATE";*/            // ^ (CONNECTIONS.NUMBER_ID = :n)
        $q = "select * from ".self::$viewStat./*" where NUMBER_ID = (select NUMBER_ID from ".self::$table.*/" where NUM = '$phone'";
        //die($q);
        $ret = self::$db->fetchAll($q);
        return $ret; //API_Utils::ansi_to_utf($ret);
        //var_dump($ret);
    }                            
    
}